from datetime import datetime
from sqlalchemy import Column, String, Text, DateTime, Integer, func
from server.db.base import Base


class PetKnowledgeBase(Base):
    """宠物专属知识库模型"""
    __tablename__ = "pet_knowledge_base"

    # 自增ID
    id = Column(Integer, autoincrement=True, primary_key=True, index=True, comment="自增ID")
    user_id = Column(String(64), nullable=False, index=True, comment="用户ID")
    pet_name = Column(String(100), comment="宠物名称（从文档提取或使用nickname）")
    workspace_id = Column(String(64), comment="工作空间ID")
    knowledge_base_id = Column(String(100), unique=True, nullable=False, index=True, comment="百炼知识库ID")
    knowledge_base_name = Column(String(200), comment="知识库名称")
    description = Column(Text, comment="知识库描述")
    document_count = Column(Integer, default=0, comment="文档数量")
    created_time = Column(DateTime, default=func.now(), comment="创建时间")
    updated_time = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")

    def __repr__(self):
        return f"<PetKnowledgeBase(id='{self.id}', user_id='{self.user_id}', pet_name='{self.pet_name}', knowledge_base_id='{self.knowledge_base_id}', document_count='{self.document_count}')>"


class PetKnowledgeFile(Base):
    """宠物知识库文件记录"""
    __tablename__ = "pet_knowledge_file"

    # 自增ID
    id = Column(Integer, autoincrement=True, primary_key=True, index=True, comment="自增ID")
    user_id = Column(String(64), nullable=False, index=True, comment="用户ID")
    knowledge_base_id = Column(String(100), nullable=False, index=True, comment="知识库ID")
    file_id = Column(String(100), unique=True, nullable=False, index=True, comment="文件ID")
    file_name = Column(String(255), comment="文件名称")
    file_url = Column(Text, comment="文件URL")
    file_status = Column(String(50), comment="文件状态：PARSING/PARSE_SUCCESS/PARSE_FAILED")
    job_id = Column(String(100), comment="追加任务ID")
    job_status = Column(String(50), comment="追加任务状态：PENDING/RUNNING/COMPLETED/FAILED")
    workspace_id = Column(String(64), comment="工作空间ID")
    request_id = Column(String(100), comment="请求ID")
    created_time = Column(DateTime, default=func.now(), comment="创建时间")
    updated_time = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")

    def __repr__(self):
        return f"<PetKnowledgeFile(id='{self.id}', user_id='{self.user_id}', knowledge_base_id='{self.knowledge_base_id}', file_id='{self.file_id}', file_name='{self.file_name}', file_status='{self.file_status}')>"

